<?php

namespace App\BO\Coupon\CouponProductSkuCheck;

use Illuminate\Support\Facades\Log;

/**
 * SKU title 检查
 * Class ProductSkuTitleCheck
 * @package App\BO\Coupon\CouponProductSkuCheck
 */
class ProductSkuTitleCheck implements ICouponProductSkuCheck
{
    public static function check(CouponProductSkuCheckMeta $checkMeta, \Closure $next)
    {
        $couponCodeEntity = $checkMeta->getCouponEntity();
        $title = $checkMeta->getProductSkuEntity()->getTitle();

        $productSkuTitleCheck = $couponCodeEntity->getProductSkuTitle();
        if ($productSkuTitleCheck) {
            $arr = explode(',', $productSkuTitleCheck);
            if (!in_array($title, $arr)) {
                Log::info('优惠券' . $couponCodeEntity->getId() . ' sku_title 检测不符合');
                $checkResultWrapper = new CheckResultWrapper($checkMeta->getCouponEntity());
                $checkResultWrapper->setMessage('此商品不能用此优惠券');
                return $checkResultWrapper;
            }
        }

        return $next($checkMeta);
    }
}
